package com.genghis.ptas.statistic.service.impl;

import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.statistic.dao.StatisticDao;
import com.genghis.ptas.statistic.entity.CourseStatistics;
import com.genghis.ptas.statistic.entity.Statistic;
import com.genghis.ptas.statistic.entity.TypeStatistic;
import com.genghis.ptas.statistic.service.StatisticService;
import com.genghis.ptas.util.ExtremeValue;
import com.genghis.ptas.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Service("statisticService")
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private StatisticDao statisticDao;

    @Override
    public List<TypeStatistic> statisticTotalSources(String ids) {
        return statisticDao.statisticTotalCourse(ids);
    }

    @Override
    public List<TypeStatistic> statisticRegisterType(String ids) {
        return statisticDao.statisticRegisterType(ids);
    }

    @Override
    public List<CourseStatistics> statisticCourse(CourseStatistics courseStatistic) {
        if (!"".equals(courseStatistic.getStatisticItem()) && null != courseStatistic.getStatisticItem())
            courseStatistic.setStatisticItem(DictConstant.DEFAULT_STATISTIC_ITEMS);
        setSort(courseStatistic);
        List<CourseStatistics> courseStatisticsList = statisticDao.statisticCourse(courseStatistic);
        ExtremeValue extremeValue = statisticDao.statisticCourseExtremeValue(courseStatistic);
        extremeValue.setGroupNum(courseStatistic.getGroupNum());
        extremeValue.setRankNumList();
        return getCourseStatisticsResult(courseStatisticsList, extremeValue);
    }

    @Override
    public List<TypeStatistic> statisticDetail(String typeName) {
        typeName = StringUtil.iso2utf(typeName);
        return statisticDao.statisticDetail(typeName);
    }

    @Override
    public List<TypeStatistic> detailResource(String typeName) {
        typeName = StringUtil.iso2utf(typeName);
        return statisticDao.detailResource(typeName);
    }

    @Override
    public List<CourseStatistics> analysisCourse(String ids) {
        return statisticDao.analysisCourse(ids);
    }

    @Override
    public List<Statistic> analysisUser(String ids) {
        return statisticDao.analysisUser(ids);
    }

    @Override
    public List<TypeStatistic> analysisUserLike(String userId) {
        return statisticDao.analysisUserLike(userId);
    }

    private List<CourseStatistics> getCourseStatisticsResult(List<CourseStatistics> courseStatisticsList, ExtremeValue extremeValue) {
        List<CourseStatistics> showResult = new ArrayList<CourseStatistics>();
        for (ExtremeValue temp : extremeValue.getRankNumList()) {
            CourseStatistics result = new CourseStatistics();
            result.setExtremeValue(temp);
            for (CourseStatistics statisticsTemp : courseStatisticsList) {
                if (statisticsTemp.getShowNum() >= temp.getLower() && statisticsTemp.getShowNum() <= temp.getUpper()) {
                    result.setShowNum(result.getShowNum() + statisticsTemp.getShowNum());
                }
            }
            showResult.add(result);
        }
        return showResult;
    }

    private void setSort(CourseStatistics courseStatistic) {
        String ranking = courseStatistic.getRankNum();
        if (null != ranking && !"".equals(ranking)) {
            String[] ranks = ranking.split("_");
            courseStatistic.setRankNum(ranks[ranks.length - 1]);
            if (ranks.length > 1)
                courseStatistic.setSort(DictConstant.ASC);
            else
                courseStatistic.setSort(DictConstant.DESC);
        } else {
            courseStatistic.setSort(DictConstant.DESC);
        }
    }

}
